package com.ist.neartweat;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

import com.ist.neartweat.service.NetworkService;
import com.ist.neartweat.service.NetworkService.NetBinder;

public class MainActivity extends Activity {
	
	NetworkService mService;
    boolean mBound = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		System.out.println("From create method");
	}

	@Override
    protected void onStart() {
        super.onStart();
        // Bind to LocalService
        Intent intent = new Intent(this, NetworkService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

	@Override
    protected void onStop() {
        super.onStop();
        // Unbind from the service
        if (mBound) {
            unbindService(mConnection);
            mBound = false;
        }
    }
	
	/** Defines callbacks for service binding, passed to bindService() */
    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
                IBinder service) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
        	System.out.println("Connected to the service");
        	NetBinder binder = (NetBinder) service;
            mService = binder.getService();
            mBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;
        }
    };

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void connectMe(View view){
		/* 
		 * Loop  until the service is created and bound with this Activity.
		 */
		while(mBound!=true){
			
		}
		
		System.out.println("Connect method called");
		/*EditText server = (EditText)findViewById(R.id.ServerIpTextBox);
		final EditText clientId = (EditText)findViewById(R.id.ClientIdTextBox);
		System.out.println("Serevr string is "+server.getText().toString()+" from connect method ");
		final String str[] =  server.getText().toString().split(":");
		System.out.println("IP Address and port are "+str[0] + " "+str[1]);*/
		
		//boolean res = mService.connect(str[0],Integer.parseInt(str[1]),clientId.getText().toString());
		final EditText clientId = (EditText)findViewById(R.id.ClientIdTextBox);
		boolean res = mService.connect("192.168.0.12",9876,clientId.getText().toString());
		//boolean res = mService.connect("194.210.230.114",9876,"raj1");
		if(res){
			System.out.println("The connection successfully established");
		}
		else{
			System.out.println("Unable to Connect");
		}
		System.out.println("starting Second Activity");
		Intent intent = new Intent(this, SecondActivity.class);
		intent.putExtra("com.ist.nearTweat.ClientId", clientId.getText().toString());
		startActivity(intent);
//		new Thread(){
//			public void run(){
//				try {
//					Socket sckt = new Socket(str[0],Integer.parseInt(str[1]));
//					InputStream iis = sckt.getInputStream();
//					OutputStream oos = sckt.getOutputStream();
//					ObjectOutputStream ops = new ObjectOutputStream(oos);
//					ops.flush();
//					ObjectInputStream ois = new ObjectInputStream(iis);
//					RegistrationMessage reg = new RegistrationMessage(clientId.getText().toString());
//					ops.writeObject(reg);
//					System.out.println("Successfully Connected");
//
//				} catch (NumberFormatException e) {
//					System.out.println("Exception while connecting");
//					e.printStackTrace();
//				} catch (UnknownHostException e) {
//					System.out.println("Exception while connecting");
//					e.printStackTrace();
//				} catch (IOException e) {
//					System.out.println("Exception while connecting");
//					e.printStackTrace();
//				}
//
//			}
//		}.start();

	}


}


